package com.glory.brepda.activity.mfg;

import android.annotation.SuppressLint;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.glory.brepda.R;
import com.glory.brepda.base.BaseActivity;
import com.glory.brepda.entity.LotInfoEntity;
import com.glory.brepda.entity.PackageEntity;
import com.glory.brepda.entity.response.BaseResponseBean;
import com.glory.brepda.utils.LotUtils;
import com.glory.brepda.utils.NetUtils;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_DialogUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StaticMembers;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.OnClick;

public class PackageDownTubeActivity extends BaseActivity {

    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.etLot)
    FreeUI_ClearEditText etLot;
    @BindView(R.id.tvCircuitName)
    TextView tvCircuitName;
    @BindView(R.id.tvCustomCode)
    TextView tvCustomCode;
    @BindView(R.id.tvPackageFrom)
    TextView tvPackageFrom;
    @BindView(R.id.tvStates)
    TextView tvStates;
    @BindView(R.id.tvGoodsNum)
    TextView tvGoodsNum;
    @BindView(R.id.etTrackOutNum)
    EditText etTrackOutNum;
    @BindView(R.id.rvSpiltLotList)
    RecyclerView rvSpiltLotList;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.tvBaseSubmit)
    TextView tvBaseSubmit;
    @BindView(R.id.tvLotQTY)
    TextView tvLotQTY;
    @BindView(R.id.tvPackageQTY)
    TextView tvPackageQTY;
    @BindView(R.id.cbReel)
    CheckBox cbReel;
    @BindView(R.id.tvTubePackageTitle)
    TextView tvTubePackageTitle;
    @BindView(R.id.tvTubeTrackOutTitle)
    TextView tvTubeTrackOutTitle;
    @BindView(R.id.cbSample)
    CheckBox cbSample;
    @BindView(R.id.tvGrade)
    TextView tvGrade;
    private int mMaxReelQTY;
    private int mPackageQTY;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_package_down;
    }

    @Override
    protected void initView() {
        String title = getIntent().getStringExtra(FreeApi_StaticMembers.SHOW_PLUGIN_KEY);
        tvTitle.setText(title);
        setMainLayout(llMain);
        tvTubePackageTitle.setText("货架盒数");
        tvTubeTrackOutTitle.setText("出站盒数");
        cbReel.setText("尾数盒先出");

        initEdit(etLot, s -> LotUtils.searchLotInfo(PackageDownTubeActivity.this, s, getSupportFragmentManager(), new LotUtils.netCallback<LotInfoEntity>() {
            @Override
            public void onSuccess(LotInfoEntity response) {
                tvCircuitName.setText(response.getPRODUCTMODEL());
                tvCustomCode.setText(response.getLOT().getCUSTOMERCODE());
                tvPackageFrom.setText(response.getLOT().getMAINMATTYPE());
                tvStates.setText(response.getLOT().getSTATE());
                tvLotQTY.setText(response.getLOT().getMAINQTY());
                requestData(s);
            }

            @Override
            public void onFail(Throwable t) {
                loadError(t, "LOTQUERY");
            }
        }));
    }

    private void requestData(String s) {
        FreeUI_EntityCallBack<BaseResponseBean<PackageEntity>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<PackageEntity>>
                (new TypeToken<BaseResponseBean<PackageEntity>>() {
                }.getType(), getSupportFragmentManager(), PackageDownTubeActivity.this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<PackageEntity>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                    mMaxReelQTY = response.body().getResponse().getBody().getMAXREELQTY();
                    etTrackOutNum.setText(String.valueOf(mMaxReelQTY));
                    mPackageQTY = response.body().getResponse().getBody().getPACKAGEQTY();
                    tvPackageQTY.setText(String.valueOf(mPackageQTY));
                    tvGrade.setText(response.body().getResponse().getBody().getBINGRADE());
                    String sampleinfo = response.body().getResponse().getBody().getSAMPLEINFO();
                    if (!sampleinfo.equals("")) {
                        showConfirmDialog(sampleinfo);
                    }
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<PackageEntity>> response) {
                super.onError(response);
                loadError(response.getException(), "PACKAGEUPSHELFTUBE");
            }
        };

        JsonObject map = new JsonObject();
        map.addProperty("ACTIONTYPE", "UNBINDSELECT");
        map.addProperty("LOTID", s);
        NetUtils.requestPostNet("CPC.PACKAGEUPSHELFTUBE", "PACKAGEUPSHELFTUBE", PackageDownTubeActivity.this, map, callBack);
    }

    @Override
    protected void initData() {

    }

    @SuppressLint("NonConstantResourceId")
    @OnClick({R.id.tvBaseSubmit, R.id.cbReel})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tvBaseSubmit:
                try {
                    if (getText(etLot).equals("") || getText(etTrackOutNum).equals("")) {
                        showToast("批次号或出站盒数不能为空", false);
                        return;
                    }

                    if (Integer.parseInt(getText(etTrackOutNum)) != mMaxReelQTY) {
                        if (Integer.parseInt(getText(etTrackOutNum)) > mPackageQTY) {
                            showToast("出站盒数不能大于总盒数", false);
                            return;
                        }
                        FreeApi_DialogUtils.getConfirmAndCancelDialog(this, "与系统推荐的盒数不符，剩余数量：" + (mPackageQTY - Integer.parseInt(getText(etTrackOutNum))) + "盒。是否确认下架?",
                                () -> packageDown(), () -> {

                                }).show(getSupportFragmentManager(), "SUBMIT_DIALOG");
                    } else {
                        FreeApi_DialogUtils.getConfirmAndCancelDialog(this, "剩余数量:" + (mPackageQTY - Integer.parseInt(getText(etTrackOutNum))) + "盒。是否确认下架？",
                                () -> packageDown(), () -> {

                                }).show(getSupportFragmentManager(), "SUBMIT_DIALOG");
                    }
                } catch (NumberFormatException e) {
                    showToast("请输入数字", false);
                }
                break;

            case R.id.cbReel:
                break;
        }

    }

    private void clear() {
        etLot.setText("");
        tvCircuitName.setText("");
        tvCustomCode.setText("");
        tvPackageFrom.setText("");
        tvStates.setText("");
        tvLotQTY.setText("");
        etTrackOutNum.setText("");
        etLot.requestFocus();
    }

    private void packageDown() {
        FreeUI_EntityCallBack<BaseResponseBean<PackageEntity>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<PackageEntity>>
                (new TypeToken<BaseResponseBean<PackageEntity>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<PackageEntity>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(), false, getSupportFragmentManager())) {
                    PackageEntity body = response.body().getResponse().getBody();
                    showConfirmDialog(body.getSAMPLEINFO().equals("") ? "下架成功" : body.getSAMPLEINFO());
                    clear();
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<PackageEntity>> response) {
                super.onError(response);
                loadError(response.getException(), "PACKAGEUPSHELFTUBE");

            }
        };
        JsonObject map = new JsonObject();
        map.addProperty("ACTIONTYPE", "UNBIND");
        map.addProperty("LOTID", getText(etLot));
        map.addProperty("CONSUMEQTY", getText(etTrackOutNum));
        map.addProperty("ISENDFLAG", cbReel.isChecked() ? "Y" : "N");
//        map.addProperty("ISSAMPLE", cbSample.isChecked() ? "Y" : "N");
        NetUtils.requestPostNet("CPC.PACKAGEUPSHELFTUBE", "PACKAGEUPSHELFTUBE", PackageDownTubeActivity.this, map, callBack);
    }
}
